package com.tmc.web.controller.intair;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.intair.IntairOrder;
import com.tmc.service.intair.IntairOrderServiceImpl;
import com.tmc.web.BaseController;

@Controller
@RequestMapping("/intairorder")
public class IntairOrderController extends BaseController {

	@Autowired
	private IntairOrderServiceImpl service;
	
	@RequestMapping(value = "/list")
	public ModelAndView findist(IntairOrder cond, Integer pageIndex, Integer pageSize, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageSize < 1 || pageSize > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<IntairOrder> page = service.findByCon(context, cond, pageIndex, pageSize);
		map.put("page", page);
		map.put("cond", cond);
		map.put("context", context);
		return new ModelAndView("views/intairorder/order-list", map);
	}
	
	@RequestMapping(value = "/list-json")
	@ResponseBody
	public String list_json(String token, IntairOrder cond, Integer pageIndex, Integer pageSize) {
		ContextInfo context = this.getContextInfo(token);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageSize < 1 || pageSize > 1000)
			pageSize = 10;
		cond.setBookId(context.getUserId());
		Pagination<IntairOrder> page = service.findByCon(context, cond, pageIndex, pageSize);
		return JSON.toJSONString(page);
	}
	
	@RequestMapping(value = "/editPage")
	public ModelAndView editPage(Long intairOrderId, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		IntairOrder intairOrder = null;
		if (intairOrderId != null)
			intairOrder = service.getById(context, intairOrderId);
		else
			intairOrder = new IntairOrder();
		map.put("intairOrder", intairOrder);
		map.put("context", context);
		return new ModelAndView("views/intairorder/order-edit", map);
	}
	
	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String save(HttpServletRequest request, IntairOrder intairOrder) {
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = service.save(context, intairOrder);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String delete(HttpServletRequest request, Long intairOrderId) throws Exception {
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = service.delete(context, intairOrderId);
		return cr.toJson();
	}
	
}
